package com.qh.yyxt.service.Impl;

import com.qh.yyxt.entity.Borrow;
import com.qh.yyxt.pojo.entity.meeting.DelayBorrow;
import com.qh.yyxt.pojo.entity.meeting.User;
import com.qh.yyxt.mapper.BorrowMapper;
import com.qh.yyxt.mapper.DelayBorrowMapper;
import com.qh.yyxt.mapper.MeetingMapper;
import com.qh.yyxt.mapper.UserMapper;
import com.qh.yyxt.pojo.BorrowDTO;
import com.qh.yyxt.pojo.BorrowPartDTO;
import com.qh.yyxt.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    private DelayBorrowMapper delayBorrowMapper;
    @Override
    public String changeBorrowById(int id, String name, String text, String starttime, String endtime)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start=new Date();
        Date end=new Date();
        try {
            start =format.parse(starttime);
            end=format.parse(endtime);
        } catch (ParseException e) {
           return "时间格式错误！";
        }
        if(start.compareTo(end)==1)
        {
            return "保存失败，结束时间大于开始时间";
        }

        //borrowMapper.changeBorrowById(id,name,text,start,end)==1
        Borrow b=borrowMapper.findById(id);
        if(b!=null)
        {
            if(b.getState().equals("已撤销"))
            {
                return "保存失败，用户已撤销";
            }else {
                if(borrowMapper.changeBorrowById(id,name,text,start,end)==1)
                    return "true";
                else{
                    return "修改失败";
                }
            }
        }else{
            return "查询不到数据！";
        }
    }

    @Override
    public int addBorrow(Borrow borrow) {
        int result = -1;
        try {
            result = borrowMapper.addBorrow(borrow);
        } catch (Exception e) {
            // e.printStackTrace();
            result = -1;
        } finally {
            return result;
        }
    }

    @Override
    public Borrow findById(int id)
    {
        return borrowMapper.findById(id);
    }

    @Override
    public int changeState(int borrowId, String state) {
        int result = -1;
        //这个按理说不会触发
        if (state.equals("已撤销")) {
            borrowMapper.changeState(borrowId,state);
          //  delayBorrowMapper.update(borrowId, state);
        }
        if (state.equals("会议结束")) {
            if (borrowMapper.findById(borrowId)!= null) {
                borrowMapper.changeState(borrowId,state);
            }
        }
        try {
            result = borrowMapper.changeState(borrowId, state);
        } catch (Exception e) {
            // e.printStackTrace();
            result = -1;
        }
        return result;
    }

    @Override
    public List<Borrow> findByMidAndState(int mid, String state)
    {
        return borrowMapper.findByMidAndState(mid, state);
    }
    @Override
    public List<BorrowDTO> findByState(String state) {
        List<Borrow> list = borrowMapper.findByState(state);
        List<BorrowDTO> dtoList = new ArrayList();
        for (Borrow b : list) {
            BorrowDTO borrowDTO = new BorrowDTO();
            DelayBorrow delayBorrow = null;
            User user = null;
            user = userMapper.findUserById(b.getUid());
            delayBorrow = delayBorrowMapper.getByBid(b.getId());
            borrowDTO.set(b, user, delayBorrow);
            dtoList.add(borrowDTO);
        }
        return dtoList;
    }
    @Override
    public List<BorrowPartDTO> findByUid(int uid) {
        List<Borrow> list = borrowMapper.findByUid(uid);
        List<BorrowPartDTO> dtoList = new ArrayList();
        for (Borrow b : list)
        {
            BorrowPartDTO borrowPartDTO = new BorrowPartDTO();
            borrowPartDTO.set(b);
            borrowPartDTO.setMidName(meetingMapper.findById(b.getMid()));
            borrowPartDTO.setUName(userMapper.findUserById(b.getUid()).getName());
            dtoList.add(borrowPartDTO);
        }
        return dtoList;
    }
    @Override
    public List<BorrowPartDTO> findAfterToday() {
        List<Borrow> list = borrowMapper.findByDate();
        List<BorrowPartDTO> dtoList = new ArrayList<>();
        for (Borrow b : list) {
            BorrowPartDTO borrowPartDTO = new BorrowPartDTO();
            DelayBorrow delayBorrow = null;
            delayBorrow = delayBorrowMapper.getByBid(b.getId());
            borrowPartDTO.set(b);
            borrowPartDTO.setMidName(meetingMapper.findById(b.getMid()));
            User user=null;
            user=userMapper.findUserById(b.getUid());
            if(user!=null)
               borrowPartDTO.setUName(user.getName());
            if (delayBorrow != null)
            {
                if (delayBorrow.getDelaystate().equals("审批通过")||delayBorrow.getDelaystate().equals("会议结束"))
                {
                    borrowPartDTO.setEndtime(delayBorrow.getDelaytime());
                }
            }
            dtoList.add(borrowPartDTO);
        }
        return dtoList;
    }
    @Override
    public Borrow findBorrowByMidAndTime(int mid, Date starttime, Date endtime) {
        return borrowMapper.findBorrowByMidAndTime(mid, starttime, endtime);
    }

    @Override
    public Borrow findBorrowByMidAndEndTime(int mid, Date endtime) {
        return borrowMapper.findBorrowByMidAndEndTime(mid, endtime);
    }

    @Override
    public Borrow findBorrowByMidAndStartTime(int mid, Date starttime) {
        return borrowMapper.findBorrowByMidAndStartTime(mid, starttime);
    }

    @Override
    public List<BorrowPartDTO> findPartAllBorrow() {
        List<Borrow>list=borrowMapper.findPartAllBorrow();
        List<BorrowPartDTO>dtoList=new ArrayList<>();
        for (Borrow b:list)
        {
            BorrowPartDTO borrowPartDTO=new BorrowPartDTO();
            borrowPartDTO.set(b);
            borrowPartDTO.setMidName(meetingMapper.findById(b.getMid()));
            borrowPartDTO.setUName(userMapper.findUserById(b.getUid()).getName());
            dtoList.add(borrowPartDTO);
        }
        if(dtoList.size()!=0)
        return dtoList;
        else{
            return null;
        }
    }

    @Override
    public BorrowDTO findOneBorrow(int id)
    {

        Borrow borrow=borrowMapper.findById(id);
        System.out.println("sql:"+borrow);
        BorrowDTO borrowDTO=new BorrowDTO();
        DelayBorrow delayBorrow=delayBorrowMapper.getByBid(id);
        borrowDTO.setMidName(meetingMapper.findById(borrow.getMid()));
        User user=userMapper.findUserById(borrow.getUid());
        borrowDTO.set(borrow,user,delayBorrow);
        if(borrow!=null){
            borrowDTO.setMidName(meetingMapper.findById(borrow.getMid()));
        }
        return borrowDTO;
    }
}
